/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Admin;

import Login.LoginServlet;
import Login.MySqlConnector;
import java.sql.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
 *
 * @author Bharath
 */
public class AdminChangePassword extends HttpServlet {
   private String query;
    private PreparedStatement pstmt;
    private int rs;
    MySqlConnector ms = new MySqlConnector();
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            System.out.println("*********&&&&**************");
            HttpSession hs = request.getSession(true);
            //String memberid = hs.getAttribute("memid").toString();
            //String sesspass = hs.getAttribute("password").toString();
            String memberid = request.getParameter("memid");
            String cpass = request.getParameter("cpass");
            String rpass = request.getParameter("rpass");
            System.out.println("member id is ......." + memberid);
            System.out.println("old password is ......." + cpass);
            System.out.println(" New Password is ......." + rpass);

            if(cpass.equals(rpass))
            {
             System.out.print("Two passwords in if condition "+cpass);
            Connection con = ms.Connect();
            query = " update registration set password = '"+cpass+"' where memberid = '"+memberid+"'; ";
            //Statement
            pstmt = con.prepareStatement(query);
            //Result Set
            rs = pstmt.executeUpdate();
            System.out.println("integer value . . .. . .  "+rs);
            //set sessions
            if(rs == 1)
            {
                out.print("<script language='javascript'>alert('successfully changed password'); location = 'AdminAccount.jsp' ;</script>");
               // getServletContext().getRequestDispatcher("/AdminAccount.jsp").forward(request, response);
            }
             else {
                      out.print("<script language='javascript'>alert('sorry error changing password');  location='AdminChangePass.jsp' ;</script>");
                     // getServletContext().getRequestDispatcher("/AdminChangePass.jsp").forward(request, response);
             }  
            }
        }catch (Exception ex) {
            Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
        }
     

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       try {
            processRequest(request, response);
        } catch (Exception ex) {
            Logger.getLogger(ManageMembers.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}